package org.example.view;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.io.IOException;

public class AddUserView extends JFrame implements Runnable{
    JTextField inputID; //输入ID
    JTextField inputPassword; //输入密码
    JTextField inputName; //输入生日
    JTextField hintMess;
    JButton submit,back;
    HandleAddUser handleAddUser;

    JCheckBox stu,tea;
    public AddUserView(){
        initView();
        registerListener();

    }
    private void initView() {
        this.setSize(480,320);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
        this.setVisible(true);
        this.setTitle("注册页面-3220608045吉治霖软件2202");

        //ID:  设置
        Font font = new Font(null,1,16);
        JLabel IDText = new JLabel("ID：");
        IDText.setFont(font);
        IDText.setBounds(118,35,55,22);
        this.getContentPane().add(IDText);
        //姓名:  设置
        JLabel  usernameText= new JLabel("姓名：");
        usernameText.setFont(font);
        usernameText.setBounds(100,75,55,22);
        this.getContentPane().add(usernameText);
        //用户类型:  设置
        JLabel userClassText = new JLabel("用户类型：");
        userClassText.setFont(font);
        userClassText.setBounds(65,115,100,22);
        this.getContentPane().add(userClassText);
        JLabel stu_c = new JLabel("学生：");
        stu_c.setFont(font);
        stu_c.setBounds(155,115,55,22);
        this.getContentPane().add(stu_c);
        JLabel tea_c = new JLabel("老师：");
        tea_c.setFont(font);
        tea_c.setBounds(255,115,55,22);
        this.getContentPane().add(tea_c);
        //密码:  设置
        JLabel passwordText = new JLabel("密码：");
        passwordText.setFont(font);
        passwordText.setBounds(100,155,55,22);
        this.getContentPane().add(passwordText);

        //ID输入框： 设置
        inputID = new JTextField();
        inputID.setBounds(190,35,150,22);
        this.getContentPane().add(inputID);
        //姓名输入框： 设置
        inputName = new JTextField();
        inputName.setBounds(190,75,150,22);
        this.getContentPane().add(inputName);
        //密码输入框： 设置
        inputPassword = new JTextField();
        inputPassword.setBounds(190,155,150,22);
        this.getContentPane().add(inputPassword);
        //用户类型  设置
        stu = new JCheckBox();
        stu.setBounds(205,115,20,22);
        this.getContentPane().add(stu);
        tea = new JCheckBox();           //选项框
        tea.setBounds(305,115,20,22);
        this.getContentPane().add(tea);
        //注册按钮
        submit = new JButton("注册");
        submit.setBounds(150,185,80,30);
        this.getContentPane().add(submit);
        //返回按钮
        back = new JButton("返回");
        back.setBounds(250,185,80,30);
        this.getContentPane().add(back);
        //提示框展示
        hintMess = new JTextField(20);
        hintMess.setEditable(false);
        hintMess.setBounds(50,230,380,30);
        this.getContentPane().add(hintMess);
    }
    private void registerListener() {
        handleAddUser = new HandleAddUser();// HandleAddWord
        handleAddUser.setView(this);// HandleAddWord
        submit.addActionListener(handleAddUser);
        back.addActionListener(handleAddUser);
    }

    @Override
    public void run() {
        String st=null;
        while(true) {
            try{
                st=handleAddUser.in.readUTF();
                System.out.println(st);
                if (st.startsWith("注册信息")) {
                    hintMess.setText(st.substring(4));
                }
            }
            catch(IOException e) {
                break;
            }
        }
    }
}
